package com.zcg.gasmanager.dao.impl;

import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import com.zcg.gasmanager.dao.RepairDao;
import com.zcg.gasmanager.model.Repair;

@Component("repairDao")
public class RepairDaoImpl implements RepairDao {

	private HibernateTemplate hibernateTemplate;
	/* (non-Javadoc)
	 * @see com.zcg.gasmanager.dao.impl.PayDao#update(com.zcg.gasmanager.model.User)
	 */
	@Override
	public void add(Repair repair)  throws RuntimeException{
		hibernateTemplate.save(repair);
	}

	/* (non-Javadoc)
	 * @see com.zcg.gasmanager.dao.impl.PayDao#queryAll()
	 */
	@Override
	public List<Repair> queryAll()  throws RuntimeException{
		List<Repair> repairs = hibernateTemplate.find("from Repair");
		return repairs;
	}


	@Override
	public List<Repair> queryLimit(final int start, final int limit)  throws RuntimeException{
		List<Repair> repairs = hibernateTemplate.executeFind(new HibernateCallback() {
			
			@Override
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				Query q = session.createQuery("from Repair");
				q.setFirstResult(start);
				q.setMaxResults(limit);
				System.out.println(start + "  "+limit);
				return q.list();
			}
		});
		return repairs;
	}
	
	@Override
	public int getCount()  throws RuntimeException{
		return (int)(long) hibernateTemplate.find("select Count(*) from Repair").get(0);
	}
	
	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}
	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}
}
